package de.tudarmstadt.gdi1.project;
//test
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import de.tudarmstadt.gdi1.project.alphabet.Alphabet;
import de.tudarmstadt.gdi1.project.alphabet.Dictionary;
import de.tudarmstadt.gdi1.project.alphabet.Distribution;
import de.tudarmstadt.gdi1.project.analyse.BacktrackingAnalyse;
import de.tudarmstadt.gdi1.project.analyse.ChoosenAndKnownPlaintext;
import de.tudarmstadt.gdi1.project.analyse.DictionaryTool;
import de.tudarmstadt.gdi1.project.analysis.EncryptionOracle;
import de.tudarmstadt.gdi1.project.analysis.ValidateDecryptionOracle;
import de.tudarmstadt.gdi1.project.analysis.monoalphabetic.MonoalphabeticCribCryptanalysis;
import de.tudarmstadt.gdi1.project.cipher.substitution.monoalphabetic.MonoalphabeticCipher;
import de.tudarmstadt.gdi1.project.substitution.CipherUtils;
import de.tudarmstadt.gdi1.project.substitution.mono.MonoalphabeticKey;
import de.tudarmstadt.gdi1.project.substitution.poly.Polyalphabetic;
import de.tudarmstadt.gdi1.project.substitution.poly.VigenereCipher;
import de.tudarmstadt.gdi1.project.test.TemplateTestCore;
import de.tudarmstadt.gdi1.project.test.TemplateTestUtils;

public class Test {
	static String s = "abcdefghijklmnopqrstuvwxyz";
/*	
	public static double getFrequency(String key) {
		double len = s.length();
		int iCount = 0;
		if (len != 0) {
			for (int i = 0; i >= 0; i = s.indexOf(key, i) + 1) {
				if (s.indexOf(key, i) >= 0) {
					iCount++;
				}else{
					break;
				}
			}
			return iCount/(len - (key.length() - 1));
		} else
			return 0;
	}

	public static LinkedList<Character> list = new LinkedList<Character>();
	public static LinkedList<Character> list2 = new LinkedList<Character>();
	public static LinkedList<Character> list3 = new LinkedList<Character>();
*/
	public static void main(String[] args){
	/*	list.add('a');
		list.add('b');
		list.add('c');
		list.add('d');
		list.add('e');
		Charset cs = new Charset(list);
		list2.add('e');
		list2.add('c');
		list2.add('a');
		list2.add('d');
		list2.add('b');
		Charset cs2 = new Charset(list2);
		list3.add('a');
		list3.add('b');
		list3.add('c');
		Charset cs3 = new Charset(list3);
		Charset[] test = new Charset[] {cs2, cs3, cs};
		Polyalphabetic test2 = new Polyalphabetic(cs);
		test2.setCiphers(cs, cs2);
	//	System.out.println(test2.encrypt("abbaaaacacba"));
		String a = "äöü " + TemplateTestUtils.ALICE_PLAIN;
		a = TemplateTestUtils.getDefaultAlphabet().normalize(a);
		CipherUtils u = new CipherUtils();
		MonoalphabeticKey m = new MonoalphabeticKey(TemplateTestUtils.getDefaultAlphabet(), "halore");
		Alphabet cs4 = u.randomizeAlphabet(TemplateTestUtils.getDefaultAlphabet());
		Charset alph = new Charset(Arrays.asList('a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'));
		DictionaryTool dic = new DictionaryTool(alph, "hallo hallo hallo");		
//		System.out.println(dic.size());
	//	System.out.println(u.toDisplay("abcdefghijklmnopqrstufwxyz1234abcdefghijklmnopqrstufwxyz1234abcdefghijklmnopqrstufwxyz1234abcdefghijklmnopqrstufwxyz1234"));*/
		BacktrackingAnalyse back = new BacktrackingAnalyse();
		Alphabet source = TemplateTestUtils.getAlphabetFrom(new char[] { 'a', 'e', 'f', 'h', 'm', 'p', 's', 't', 'v', 'z' });
		final Alphabet key = TemplateTestUtils.getAlphabetFrom(new char[] { 'f', 'm', 'a', 'p', 'e', 'v', 'z', 's', 't', 'h' });

		Alphabet sourceWithBlank = TemplateTestUtils.getAlphabetFrom(new char[] { 'a', 'e', 'f', 'h', 'm', 'p', 's', 't', 'v', 'z', ' ' });

		final Dictionary dictionary = TemplateTestCore.getFactory().getDictionaryInstance(source,
				sourceWithBlank.normalize(TemplateTestUtils.ALICE));
		final Distribution distribution = TemplateTestCore.getFactory().getDistributionInstance(source,
				sourceWithBlank.normalize(TemplateTestUtils.ALICE), 3);

		final MonoalphabeticCribCryptanalysis ca = TemplateTestCore.getFactory().getMonoalphabeticCribCryptanalysisInstance();

		MonoalphabeticCipher cipher = TemplateTestCore.getFactory().getMonoalphabeticCipherInstance(source, key);
		final String plaintext = source.normalize(TemplateTestUtils.ALICE_PLAIN);
		final String ciphertext = cipher.encrypt(plaintext);

		final List<String> cribs = new ArrayList<>();


		/*System.out.println(back.knownCiphertextAttack(ciphertext, distribution, dictionary, cribs, new ValidateDecryptionOracle() {
			@Override
			public boolean isCorrect(String p) {
				return plaintext.equals(p);
			}
		}));*/
	}
	
}
